<?php
class Session 
{
    const COOKIE_EXPIRED_TIME      = 300;
    public static $identityKey     = "user_id";
    public static $adminSessionKey = "admin_user_id";

    /**
     * when want to start session, use this method
     */
    public static function start($session_id = NULL, $check_cookie = true, $end = true)     
    {    
        if (!isset($_SESSION) || !$_SESSION)
        {
            if ($session_id)
            {
                session_id($session_id);
            }

            session_start();
        }

        $identity_key = self::$identityKey;
        $session_value = self::get($identity_key);

        if ($session_value && !$check_cookie)
        {
            return true; 
        }

        $cookie_value  = isset($_COOKIE[$identity_key]) ? $_COOKIE[$identity_key] : null;

        if (!$cookie_value || !$session_value || $cookie_value != self::encryptIdentityValue($session_value)) 
        {
            if ($end)
            {
                self::end();
            }
            else
            {
                @session_unset();
            }
        }
    }
    
    /**
     * when user login, use this method
     * @param mixed $identity_value user_id
     * @param mixed $info user info
     *
     */
    public static function begin($identity_value, $info = NULL) 
    {
        $identity_key = self::$identityKey;

        $cookie_value = isset($_COOKIE[$identity_key]) ? $_COOKIE[$identity_key] : null;

        if (!$cookie_value || !$identity_value || $cookie_value != self::encryptIdentityValue($identity_value)) 
        {
            setcookie($identity_key, self::encryptIdentityValue($identity_value), time() + self::COOKIE_EXPIRED_TIME, "/");
        }       

        if (!isset($_SESSION) || !$_SESSION) 
        {
            session_start();
        }

        self::set($identity_key, $identity_value);

        if ($info)
        {
            self::set($identity_key."_info", $info);
        }

        return true;
    }

    public static function isLogin($check_cookie = true) 
    {
        if (self::getUserId($check_cookie))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static function getUserId($check_cookie = true)
    {
        $info = self::getUserInfo($check_cookie);

        return $info["user_id"];
    }

	public static function getUserInfo($check_cookie = true)
	{
        $identity_key = self::$identityKey;

        $session_value = self::get($identity_key);
        if (!$session_value)
        {
            return false;
        } 

        if (!$check_cookie)
        {
            return true;
        }

        $cookie_value = isset($_COOKIE[$identity_key]) ? $_COOKIE[$identity_key] : null;

        if (!$cookie_value || !$session_value|| $cookie_value != self::encryptIdentityValue($session_value)) 
        {
            return NULL;
        }

        $info = self::get($identity_key."_info");

        return $info;
	}

    public static function getAdminUserId()
    {
        return self::get(self::$adminSessionKey);
    }

    public static function setAdminUserId($user_id)
    {
        return self::set(self::$adminSessionKey, $user_id);
    }

    public static function end() 
    {
        $identity_key = self::$identityKey;
        setcookie($identity_key, 0, time() - 3600, "/");

        if (isset($_SESSION)) 
        {
            @session_unset();
            @session_destroy();
        }   
    }

    public static function set($key, $value)
    {
        $_SESSION[$key] = $value;

        return true;
    }

    public static function get($key) 
    {
        return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
    }

    public static function encryptIdentityValue($identity_value) 
    {
        return md5(crc32($identity_value));
    }

} //end class
